/**
 * <pre>
 * Copyright 2009 openmaximo.net
 * 
 * Licensed under the Apache License, Version 2.0 (the &quot;License&quot;);
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 *   http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by Datalicable law or agreed to in writing, software
 * distributed under the License is distributed on an &quot;AS IS&quot; BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License.
 * </pre>
 */
package net.openmaximo.maven.plugins;

import java.io.File;
import java.io.IOException;
import java.util.HashMap;

import net.openmaximo.log.Log4jLogger;
import net.openmaximo.maven.plugins.templates.Template;
import net.openmaximo.util.PackageUtil;

import org.apache.log4j.Logger;
import org.apache.maven.plugin.MojoExecutionException;

/**
 * <p>
 * MOJO that generates IBM Maximo Data Bean classes.
 * </p>
 * <h2>Override methods available</h2>
 * <p>
 * <ul>
 * <li><b>getMboSet</b> - To be used as the Bean of a Table. Allows the
 * customization of the MboSet to be loaded into the table.</li>
 * </ul>
 * </p>
 * <hr />
 * <h2>Latest Modification</h2>
 * <p>
 * $Date: 2009-12-30 11:39:52 +0000 (Wed, 30 Dec 2009) $
 * </p>
 * <p>
 * $Rev: 60 $
 * </p>
 * 
 * @author openmaximo.net
 * @goal databean
 * @since 0.2
 * @version 1.0
 * @version 1.0
 */
public class DataBean extends Maximo {
  /**
   * 
   */
  private static final Logger lLogger = Log4jLogger.getLogger(DataBean.class);
  /**
   * <p>
   * Bean we'll be extending.
   * </p>
   * 
   * @parameter expression="${bean.base}"
   * @required
   */
  private String baseBean;
  /**
   * <p>
   * package and classname of the new Bean.
   * </p>
   * 
   * @parameter expression="${bean.new}"
   * @required
   */
  private String newBean;

  // ------------------------ GETTERS N SETTERS --------------------------
  /**
   * @return the baseBean
   */
  public String getBaseBean() {
    return baseBean;
  }

  /**
   * @param baseBean
   *          the baseBean to set
   */
  public void setBaseBean(String baseBean) {
    this.baseBean = baseBean;
  }

  /**
   * @return the newBean
   */
  public String getNewBean() {
    return newBean;
  }

  /**
   * @param newBean
   *          the newBean to set
   */
  public void setNewBean(String newBean) {
    this.newBean = newBean;
  }

  // ------------------------ /GETTERS N SETTERS -------------------------
  /**
   * <p>
   * Executes the plugin.
   * </p>
   */
  public void execute() throws MojoExecutionException {
    lLogger.info(".execute() started.");
    if (lLogger.isDebugEnabled()) {
      lLogger.debug("newMbo:" + newBean);
      lLogger.debug("baseMbo:" + baseBean);
      lLogger.debug("outputDirectory:" + outputDirectory);
      lLogger.debug("PluginContext:"
          + this.getPluginContext().keySet().toString());
    }
    if (newBean == null) {
      throw new MojoExecutionException(
          "The new MBO must not be empty. Please use the -DnewMbo=... parameter.");
    }
    File f = outputDirectory;
    if (!f.exists()) {
      f.mkdirs();
    } else if (f.isFile()) {
      throw new MojoExecutionException("Target folder cannot be a file.");
    }
    // Do the thing
    try {
      String beanName = PackageUtil.getClassName(newBean);
      // Extract the package name from the newMbo variable.
      String packageName = newBean.replaceFirst(".".concat(beanName), "");
      String packageFolder = PackageUtil.packageToFolder(packageName);
      if (lLogger.isDebugEnabled()) {
        lLogger.debug("beanName:" + beanName);
        lLogger.debug("packageName:" + packageName);
        lLogger.debug("packageFolder:" + packageFolder);
      }
      HashMap patternsHM = Template.setPatterns(beanName, this.getBaseBean(),
          packageName, this.getClass().getSimpleName(), this
              .getOverrideMethods(), this.getClass());
      String outputFolderPath = outputDirectory.getPath().concat("/").concat(
          packageFolder);
      if (lLogger.isDebugEnabled()) {
        lLogger.debug("outputFolderPath:" + outputFolderPath);
        // lLogger.debug("outputFilePath:" + outputFilePath);
      }
      String outputClassName = null;
      Template templateData = null;
      // MBO
      outputClassName = beanName;
      templateData = new Template(this.getClass(), "DataBean.java",
          outputFolderPath, outputClassName, this.getCharEncoding(), patternsHM);
      templateData.applyTemplate();
    } catch (IOException e) {
      // I don't care if there is an exception at this point.
      e.printStackTrace();
    }
    lLogger.info(".execute() finished.");
  }
}
